package utils;



import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;



import java.security.Key;
import java.util.Date;
import java.util.Map;


public class JwtUtils {
    //设置过期时间
    private static final long EXPIRATION_DATE= 60*60*1000;//1小时

    private final static String secretString = "89dhbG4EAH95p9LPUAXeE3nAmc4nFQKbZKoV/8lVGos=";
    private final static Key key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secretString ));
    //生成令牌
//    @Deprecated//弃用
    //它接受一个参数，即一个Map<String, Object>类型的claim，这个Map通常用于存储JWT的声明（claims）。
    //方法返回一个String类型的JWT令牌。
    public static String genToken(Map<String ,Object> claim) {

        return Jwts.builder().setClaims(claim).
                setExpiration(new Date(System.currentTimeMillis()+EXPIRATION_DATE)).signWith(key).compact();

    }

    //解析

    public static Claims parseToken(String token) {

        JwtParser build = Jwts.parserBuilder().setSigningKey(key).build();
        Claims body = null;
        try {
            body = build.parseClaimsJws(token).getBody();
        }catch (ExpiredJwtException e) {
            System.out.println("令牌过期,校验失败,token:"+token);

        } catch (Exception e) {
            System.out.println("令牌校验失败:token:"+token);

        }
        return body;

    }

    //校验令牌
    public static boolean checkToken(String token) {

        Claims body = parseToken(token);
        if(body == null) {
            return false;
        }
        return true;


    }

    //获取学生id
    public static Integer getStuIdFromToken(String token,String stuId) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(stuId);
        }
        return null;
    }

    //获取学生姓名
    public static String getStuNameFromToken(String token,String stuName) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(stuName);
        }
        return null;
    }

    //获取学生年龄
    public static Integer getStuAgeFromToken(String token,String stuAge) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(stuAge);
        }
        return null;
    }

    //获取学生性别
    public static Integer getStuSexFromToken(String token,String stuSex) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(stuSex);
        }
        return null;
    }

    //获取学生头像路径
    public static String getStuImgFromToken(String token,String stuImg) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(stuImg);
        }
        return null;
    }

    //获取学生电话
    public static String getStuPhoneFromToken(String token,String stuPhone) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(stuPhone);
        }
        return null;
    }

    //获取学生邮箱
    public static String getStuEmailFromToken(String token,String stuEmail) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(stuEmail);
        }
        return null;
    }

    //获取学生楼栋
    public static Integer getStuBuildIdFromToken(String token,String stuBuildId) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(stuBuildId);
        }
        return null;
    }

    //获取学生宿舍号
    public static Integer getStuDormitoryFromToken(String token,String stuDormitory) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(stuDormitory);
        }
        return null;
    }

    //获取学生床位号
    public static Integer getStuBedFromToken(String token,String stuBed) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(stuBed);
        }
        return null;
    }

    //获取学生权限
    public static Integer getStuLimitsFromToken(String token,String stuLimits) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(stuLimits);
        }
        return null;
    }

    //获取学生学院
    public static String getStuCollegeFromToken(String token,String stuCollege) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(stuCollege);
        }
        return null;
    }

    //获取学生专业
    public static String getStuProfessionFromToken(String token,String stuProfession) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(stuProfession);
        }
        return null;
    }

    //获取学生电费
    public static Integer getStuElectricFromToken(String token,String stuElectric) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(stuElectric);
        }
        return null;
    }

    //获取学生宿舍费
    public static Integer getStuAccommodationFromToken(String token,String stuAccommodation) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(stuAccommodation);
        }
        return null;
    }

    //获取学生op
    public static String getStuOpFromToken(String token,String stuOp) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(stuOp);
        }
        return null;
    }

    //获取教师id
    public static Integer getTeaIdFromToken(String token,String teaId) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(teaId);
        }
        return null;
    }

    //获取教师姓名
    public static String getTeaNameFromToken(String token,String teaName) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(teaName);
        }
        return null;
    }

    //获取教师年龄
    public static Integer getTeaAgeFromToken(String token,String teaAge) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(teaAge);
        }
        return null;
    }

    //获取教师性别
    public static Integer getTeaSexFromToken(String token,String teaSex) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(teaSex);
        }
        return null;
    }

    //获取教师头像路径
    public static String getTeaImgFromToken(String token,String teaImg) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(teaImg);
        }
        return null;
    }

    //获取教师电话
    public static String getTeaPhoneFromToken(String token,String teaPhone) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(teaPhone);
        }
        return null;
    }

    //获取教师邮箱
    public static String getTeaEmailFromToken(String token,String teaEmail) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(teaEmail);
        }
        return null;
    }

    //获取教师权限
    public static Integer getTeaLimitsFromToken(String token,String teaLimits) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(teaLimits);
        }
        return null;
    }

    //获取教师op
    public static String getTeaOpFromToken(String token,String teaOp) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(teaOp);
        }
        return null;
    }


    //获取宿管id
    public static Integer getHouIdFromToken(String token,String houId) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(houId);
        }
        return null;
    }

    //获取宿管姓名
    public static String getHouNameFromToken(String token,String houName) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(houName);
        }
        return null;
    }

    //获取宿管年龄
    public static Integer getHouAgeFromToken(String token,String houAge) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(houAge);
        }
        return null;
    }

    //获取宿管性别
    public static Integer getHouSexFromToken(String token,String houSex) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(houSex);
        }
        return null;
    }

    //获取宿管头像路径
    public static String getHouImgFromToken(String token,String houImg) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(houImg);
        }
        return null;
    }

    //获取宿管电话
    public static String getHouPhoneFromToken(String token,String houPhone) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(houPhone);
        }
        return null;
    }

    //获取宿管邮箱
    public static String getHouEmailFromToken(String token,String houEmail) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(houEmail);
        }
        return null;
    }

    //获取宿管楼栋
    public static Integer getHouBuildIdFromToken(String token,String houBuildId) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(houBuildId);
        }
        return null;
    }

    //获取宿管权限
    public static Integer getHouLimitsFromToken(String token,String houLimits) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(houLimits);
        }
        return null;
    }

    //获取宿管op
    public static String getHouOpFromToken(String token,String houOp) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(houOp);
        }
        return null;
    }

    //获取管理员id
    public static Integer getManIdFromToken(String token,String manId) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(manId);
        }
        return null;
    }

    //获取管理员姓名
    public static String getManNameFromToken(String token,String manName) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(manName);
        }
        return null;
    }

    //获取管理员电话
    public static String getManPhoneFromToken(String token,String manPhone) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(manPhone);
        }
        return null;
    }

    //获取管理员性别
    public static Integer getManSexFromToken(String token,String manSex) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(manSex);
        }
        return null;
    }

    //获取管理员邮箱
    public static String getManEmailFromToken(String token,String manEmail) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(manEmail);
        }
        return null;
    }

    //获取管理员权限
    public static Integer getManLimitsFromToken(String token,String manLimits) {

        Claims body = parseToken(token);
        if(body != null) {
            return (Integer) body.get(manLimits);
        }
        return null;
    }

    //获取管理员op
    public static String getManOPFromToken(String token,String manOp) {

        Claims body = parseToken(token);
        if(body != null) {
            return (String) body.get(manOp);
        }
        return null;
    }
}
